#ifndef INSTALLEDSERVICESDB_H
#define INSTALLEDSERVICESDB_H

#include "CoreFarmAPI.h"


/////////////////////////////////////////////////
//// Installed Services node and methods ////////
/////////////////////////////////////////////////

typedef struct InstalledServicesNode{

	char serviceName[SERVICE_NAME_LENGTH];
	int serviceID;
	int serviceVersion;
	int serverID;
	struct InstalledServicesNode *next;

} InstalledServicesNode;

InstalledServicesNode* installedServicesNodeConstructor(){
	InstalledServicesNode *thisNode;
	thisNode = (InstalledServicesNode*)malloc(sizeof(InstalledServicesNode));
	assertMem(thisNode);

	thisNode->serverID = 0;
	thisNode->serviceID = 0;
	thisNode->serviceVersion = 0;
	thisNode->next = NULL;
	return thisNode;
}
void installedServicesNodeSetServiceName(InstalledServicesNode *thisNode, char *_name){ strcpy(thisNode->serviceName, _name); }
void installedServicesNodeSetServiceID(InstalledServicesNode *thisNode, int _ID){ thisNode->serviceID = _ID; }
void installedServicesNodeSetServiceVersion(InstalledServicesNode *thisNode, int _ID){ thisNode->serviceVersion = _ID; }
void installedServicesNodeSetServerID(InstalledServicesNode *thisNode, int _ID){ thisNode->serverID= _ID; }
void installedServicesNodeSetNext(InstalledServicesNode *thisNode, InstalledServicesNode *next){ thisNode->next = next; }
char* installedServicesNodeGetServiceName(InstalledServicesNode *thisNode){ return thisNode->serviceName; }
int installedServicesNodeGetServiceID(InstalledServicesNode *thisNode){ return thisNode->serviceID; }
int installedServicesNodeGetServiceVersion(InstalledServicesNode *thisNode){ return thisNode->serviceVersion; }
int installedServicesNodeGetServerID(InstalledServicesNode *thisNode){ return thisNode->serverID; }
InstalledServicesNode* installedServicesNodeGetNext(InstalledServicesNode *thisNode){ return thisNode->next; }

///////////////////////////////////////////////////////////////
// InstalledServices Database list structure and 'methods'/////
///////////////////////////////////////////////////////////////

typedef struct InstalledServicesDB{
	int size;
	InstalledServicesNode *head;

} InstalledServicesDB;
InstalledServicesDB *installedServices_DB = NULL;

void installedServicesDBConstructor(){
	installedServices_DB = (InstalledServicesDB*)malloc(sizeof(InstalledServicesDB));
	assertMem( installedServices_DB );

	installedServices_DB->size = 0;
	installedServices_DB->head = NULL;
}

void installedServicesDBSetHead(InstalledServicesNode *head){ installedServices_DB->head = head; }
InstalledServicesNode* installedServicesDBGetHead(){ return installedServices_DB->head; }
int installedServicesDBIsEmpty(){ return (installedServices_DB->size==0); }

void installedServicesDBPush(char *_name, int _svcID, int _svn, int _svrID ){

	InstalledServicesNode *newInstalledServices = installedServicesNodeConstructor();

	installedServicesNodeSetServiceName( newInstalledServices, _name );
	installedServicesNodeSetServiceID( newInstalledServices, _svcID );
	installedServicesNodeSetServiceVersion( newInstalledServices, _svn );
	installedServicesNodeSetServerID( newInstalledServices, _svrID );

	if( !installedServices_DB )
		installedServicesDBConstructor();

	if( !installedServicesDBIsEmpty() )
		newInstalledServices->next = installedServices_DB->head;

	installedServices_DB->head = newInstalledServices;
	installedServices_DB->size++;
}

int installedServiceExists(int serverID, int serviceID){
	InstalledServicesNode *currNode = NULL;

	if( installedServices_DB )
		currNode = installedServices_DB->head;
	while( currNode )
	{
		if( serverID == currNode->serverID && serviceID == currNode->serviceID )
			return 1;
		currNode = currNode->next;
	}
	
	return 0;
}

int installedServicesDBRemove(int serviceID, int serverID){
	InstalledServicesNode *currNode = NULL, *prevNode = NULL;

	if( installedServices_DB )
		currNode = installedServices_DB->head;
	while( currNode )
	{
		if( serverID == currNode->serverID && serviceID == currNode->serviceID )
		{
			if(prevNode)
				prevNode->next = currNode->next;
			else
				installedServices_DB->head = currNode->next;
			return 1;
		}
		prevNode = currNode;
		currNode = currNode->next;
	}
	
	return 0;
}

void installedServicesPrintAll(){
	InstalledServicesNode *currNode=NULL;
	
	if( installedServices_DB )
		currNode = installedServices_DB->head;
	while( currNode ){
		printf("Service Name: %s \nService ID: %d\nService Version: %d\nServer ID: %d\n", currNode->serviceName, currNode->serviceID,currNode->serviceVersion,currNode->serverID);
		currNode = currNode->next;
	}
}

#endif